/*
 * Copyright 2013 Robert Peszek.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

	package fpig.lists.structures

import fpig.lists.mixins.*
import fpig.lists.functions.InAndOutOfF

/**
* Simple Functional Lazy List Implementation, includes streams 
*
* http://code.google.com/p/fpiglet/
* @author Robert Peszek
*/
@Mixin([OoListOperationsPart, OoListInAndOutOfFPart])
class LazyList<T> extends FunList<T>{

    private static final LazyList PROTOTYPE = new LazyList()
    static LazyList lazyStreamFrom(n) {
        PROTOTYPE.build(n, { lazyStreamFrom(n+1) })
    }

    boolean isEmpty() { this._empty }
    LazyList<T> prepend(T val) {
        build(val, { this })
    }


    //used by Functions
    T getHead() {
        _head
    }
    LazyList getTail() {
        def res = tailEvaluator ? tailEvaluator.call() : null
        res as LazyList //Groovy has problem with mixin casts otherwise
    }
    def build(T val, Closure tailEval) { new LazyList<T>(val, tailEval) }

    //privates
    private T _head //potenitially public
    private boolean _empty = false
    private Closure tailEvaluator
    private LazyList(T head, Closure tailEvaluator) { this._head=head; this.tailEvaluator=tailEvaluator }
    private LazyList() { this._empty = true }

}
